﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LibUtilities.dal;
using System.Data;
using System.Data.SqlClient;

namespace CLibArtreve.dal
{
    public class ArtPictureDAL : SqlDatabase
    {
        #region CONSTRUCTORS
        public ArtPictureDAL(string connectionString) : base(connectionString) { }
        #endregion

        #region OVERRIDE METHODS
        public override string ToString()
        {
            return "DATA ACCES LAYER: " + this.GetType().ToString();
        }
        #endregion        
        
        #region GET METHODS
        public DataSet GetAllArtPictures()
        {
            try
            {
                return ExecuteDataSetWithCommonQuery(Queries.SELECTALLARTPICTURES, null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }        
        public DataSet GetArtPictureById(Int64? id)
        {
            try
            {
                // MAKE A LIST OF PARAMETERS
                List<SqlParameter> parameters = new List<SqlParameter>();

                // MAKE PARAMETER
                SqlParameter parameter = new SqlParameter();
                parameter.DbType = DbType.Int64;
                parameter.Direction = ParameterDirection.Input;
                parameter.ParameterName = "artpicture_id";
                parameter.Value = id;
                parameters.Add(parameter);

                // EXECUTE
                return ExecuteDataSetWithCommonQuery(Queries.SELECTARTPICTUREBYPICTUREID, parameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public DataSet GetArtPictureByArtId(Int64? id)
        {
            try
            {
                // MAKE A LIST OF PARAMETERS
                List<SqlParameter> parameters = new List<SqlParameter>();

                // MAKE PARAMETER
                SqlParameter parameter = new SqlParameter();
                parameter.DbType = DbType.Int64;
                parameter.Direction = ParameterDirection.Input;
                parameter.ParameterName = "arts_art_id";
                parameter.Value = id;
                parameters.Add(parameter);

                // EXECUTE
                return ExecuteDataSetWithCommonQuery(Queries.SELECTARTPICTUREBYARTID, parameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region INSERT METHODS
        public int InsertArtPicture(string artPictureImg, Int64 artId)
        {
            try
            {
                // MAKE SOME PARAMETERS
                List<SqlParameter> parameters = new List<SqlParameter>();

                // MAKE PARAMETERS
                SqlParameter parameter = new SqlParameter();
                parameter.DbType = DbType.String;
                parameter.Direction = ParameterDirection.Input;
                parameter.ParameterName = "artpicture_image";
                parameter.Value = artPictureImg;
                parameters.Add(parameter);

                parameter = new SqlParameter();
                parameter.DbType = DbType.String;
                parameter.Direction = ParameterDirection.Input;
                parameter.ParameterName = "arts_art_id";
                parameter.Value = artId;
                parameters.Add(parameter);

                // EXECUTE
                int p_art = ExecuteNonQueryWithCommonQuery(Queries.INSERTARTPICTURE, parameters, true);
                if (p_art > 0)
                {
                    p_art = Convert.ToInt32(ExecuteScalarWithCommonQuery<decimal>(Queries.SELECTIDENTITYAFTERINSERTSQLSERVER));
                }
                return p_art;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region UPDATE METHODS
        public int UpdateArtPicture(string artPictureImg, Int64? artId)
        {
            try
            {
                //MAKE SOME PARAMETERS
                List<SqlParameter> parameters = new List<SqlParameter>();

                SqlParameter parameter = new SqlParameter();
                if (artId != null)
                {
                    parameter = new SqlParameter();
                    parameter.DbType = DbType.String;
                    parameter.Direction = ParameterDirection.Input;
                    parameter.ParameterName = "arts_art_id";
                    parameter.Value = artId;
                    parameters.Add(parameter);
                }
                else
                {
                    parameter = new SqlParameter();
                    parameter.DbType = DbType.Int64;
                    parameter.Direction = ParameterDirection.Input;
                    parameter.ParameterName = "arts_art_id";
                    parameter.Value = DBNull.Value;
                    parameters.Add(parameter);
                }

                parameter = new SqlParameter();
                parameter.DbType = DbType.String;
                parameter.Direction = ParameterDirection.Input;
                parameter.ParameterName = "artpicture_image";
                parameter.Value = artPictureImg;
                parameters.Add(parameter);

                return ExecuteNonQueryWithCommonQuery(Queries.UPDATEARTPICTURE, parameters, true);

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        #endregion

        #region DELETE METHODS
        public int DeleteArtPicture(Int64 ArtId)
        {
            try
            {
                //MAKE SOME PARAMETERS
                List<SqlParameter> parameters = new List<SqlParameter>();

                //MAKE PARAMETERS
                SqlParameter parameter = new SqlParameter();
                parameter.DbType = DbType.Int64;
                parameter.Direction = ParameterDirection.Input;
                parameter.ParameterName = "arts_art_id";
                parameter.Value = ArtId;
                parameters.Add(parameter);

                //EXECUTE
                int p_userArt = ExecuteNonQueryWithCommonQuery(Queries.DELETERARTPICTURE, parameters, true);

                return p_userArt;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        #endregion
    }
}
